@layout('layouts.system')

@section('content')
	<div class="page-header">
	    <h3>{{ Str::upper(Lang::line('admin.sysuser')->get()) }}</h3>
	</div>
  	<div class="row-fluid">
  		<div class="span6">
			{{ Form::search_open('#', 'POST', array('id' => 'searchUserForm','class' => 'span12'));}}
			{{ Form::append_buttons(Form::span12_text('searchval',null, array('id'=>'searchval', 'required','placeholder'=> Str::title(Lang::line('global.type')->get().' '.Lang::line('admin.idorname')->get()))), array(Form::button('<i class="icon-search"></i>',array('id'=>'searchButton')))) }}
			{{ Form::close(); }}
  		</div>
  		<div class="span5 pull-right">
  			<a href="#addUserModal" role="button" class="btn pull-right" data-toggle="modal" style='margin-bottom:10px'><i class="icon-plus"></i>&nbsp;{{ Str::title(Lang::line('admin.reguser')->get()) }}</a>
  		</div>
  	</div>
	<div id="userList" class="rows-fluid show-grid">
		{{ $userlist }}
	</div>
	<div id="addUserModal" class="modal container hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-header">
	    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
	    <h3 id="myModalLabel">{{ Str::title(Lang::line('admin.reguser')->get()) }}</h3>
	  </div>
	  <div class="modal-body">
		{{ Form::horizontal_open_for_files('admin/user/register', 'POST', array('id' => 'addUserForm')) }}

	    <h4>{{ Str::title(__('admin.personalinfo')->get()) }}</h4>
	    <div class="row">
			<div class="span5">
			  	<div class="control-group">
				    <label class="control-label" for="fullname">{{ Str::title(__('admin.datestart')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span2_text('wp_tarikh_mula',null,array('required','id'=>'wp_tarikh_mula','placeholder'=>Str::title(__('global.type')->get().' '.__('admin.datestart')->get()))) }}
				      <span class="help-inline">&nbsp;<span class="text-error"><strong>*</strong><small>{{ __('global.required')->get() }}</small></span></span>
				      <!-- {{ Form::medium_text('wp_tarikh_tamat',null,array('id'=>'wp_tarikh_tamat','placeholder'=>Str::title(Lang::line('global.type')->get().' '.Lang::line('admin.dateend')->get()))) }} -->
				    </div>
			  	</div>
			  	<div class="control-group">
				    <label class="control-label" for="fullname">{{ Str::title(__('admin.dateend')->get()) }}</label>
				    <div class="controls">
				      <!--{{ Form::span2_text('dateend',null,array('placeholder'=>Str::title(Lang::line('global.type')->get().' '.__('admin.dateend')->get()))) }}-->
					  {{ Form::span2_text('wp_tarikh_tamat',null,array('required','id'=>'wp_tarikh_tamat','placeholder'=>Str::title(__('global.type')->get().' '.__('admin.dateend')->get()))) }}
				    </div>
			  	</div>
			  	<div class="control-group">
				    <label class="control-label" for="fullname">{{ Str::title(__('admin.tagid')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span2_text('sup_id_pengguna',null,array('required','placeholder'=>Str::title(__('global.type')->get().' '.__('admin.tagid')->get()))) }}
				    	<span class="help-inline">&nbsp;<span class="text-error"><strong>*</strong><small>{{ __('global.required')->get() }}</small></span></span>
				    </div>
			  	</div>
			  	<div class="control-group">
				    <label class="control-label" for="fullname">{{ Str::title(__('admin.fullname')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span3_text('sup_nama',null,array('required','placeholder'=>Str::title(__('global.type')->get().' '.__('admin.fullname')->get()))) }}
				    	<span class="help-inline">&nbsp;<span class="text-error"><strong>*</strong><small>{{ __('global.required')->get() }}</small></span></span>
				    </div>
			  	</div>
				<div class="control-group">
				    <label class="control-label" for="emel">{{ Str::title(__('admin.activeemel')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span3_email('sup_emel',null,array('placeholder'=>Str::title(__('global.type')->get().' '.__('admin.activeemel')->get()),'required')) }}
				    	<span class="help-inline">&nbsp;<span class="text-error"><strong>*</strong><small>{{ __('global.required')->get() }}</small></span></span>
				    </div>
			  	</div>
				<div class="control-group">
				    <label class="control-label" for="icno">{{ Str::title(__('admin.telno')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span2_text('sup_tel',null,array('placeholder'=>Str::title(__('global.type')->get().' '.__('admin.telno')->get()))) }}
				    </div>
			  	</div>
				<div class="control-group">
				    <label class="control-label" for="icno">{{ Str::title(__('admin.idno')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span2_text('sup_no_ic',null,array('placeholder'=>Str::title(__('global.type')->get().' '.__('admin.idno')->get()),'required')) }}
				    	<span class="help-inline">&nbsp;<span class="text-error"><strong>*</strong><small>{{ __('global.required')->get() }}</small></span></span>
				    </div>
			  	</div>
				<div class="control-group">
				    <label class="control-label" for="icno">{{ Str::title(__('admin.staffno')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span2_text('sup_no_pekerja',null,array('placeholder'=>Str::title(__('global.type')->get().' '.__('admin.staffno')->get()),'required')) }}
				    </div>
			  	</div>
	  		</div>
			<div class="span5">
				<div class="control-group">
				    <label class="control-label" for="role">{{ Str::title(__('admin.staffgroup')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span3_select('wjkp_no_auto', $allgroup); }}
					  <span class="help-inline">&nbsp;<span class="text-error"><strong>*</strong><small>{{ __('global.required')->get() }}</small></span></span>
				    </div>
			  	</div>
				<div class="control-group">
				    <label class="control-label" for="role">{{ Str::title(__('admin.department')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span4_select('wpj_no_auto', $alldept); }}
				    </div>
			  	</div>
				<div class="control-group">
				    <label class="control-label" for="role">{{ Str::title(__('admin.division')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span4_select('wpb_no_auto', $division); }}
				    </div>
			  	</div>
				<div class="control-group">
				    <label class="control-label" for="role">{{ Str::title(__('admin.unit')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span4_select('wpu_no_auto', $unit); }}
				    </div>
			  	</div>
				<div class="control-group">
				    <label class="control-label" for="supervisor1">{{ Str::title(__('admin.supervisor1')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span3_select('sup_penyelia_satu', $supervisorone); }}
				    	<!-- <span class="help-inline">&nbsp;<span class="text-error"><strong>*</strong><small>{{ __('global.required')->get() }}</small></span></span> -->
				    </div>
			  	</div>
				<div class="control-group">
				    <label class="control-label" for="supervisor2">{{ Str::title(__('admin.supervisor2')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span3_select('sup_penyelia_dua', $supervisortwo); }}
				    	<!-- <span class="help-inline">&nbsp;<span class="text-error"><strong>*</strong><small>{{ __('global.required')->get() }}</small></span></span> -->
				    </div>
			  	</div>
				<div class="control-group">
				    <label class="control-label" for="role">{{ Str::title(__('admin.navuserrole')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span2_select('sur_id', $allrole); }}
				    	<span class="help-inline">&nbsp;<span class="text-error"><strong>*</strong><small>{{ __('global.required')->get() }}</small></span></span>
				    </div>
			  	</div>
				<div class="control-group">
					<div class="controls">
					{{ Form::labelled_checkbox('sup_tidak_kelihatan', 'Tidak termasuk di dalam sistem'); }}
					</div>
				</div>
				<div class="control-group" id="pengguna">
					<div class="controls">
					{{ Form::labelled_checkbox('sup_is_supervisor', 'Adalah Penyelia'); }}
					</div>
				</div>
			</div>
	  	</div>
		<div class="row">
			<div class="span5">
			  	<h4>{{ Str::title(__('global.logininfo')->get()) }}</h4>
			  	<div class="control-group">
				    <label class="control-label" for="role">{{ Str::title(__('global.username')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span2_text('su_username',null,array('required','class' => 'disabled','readonly')) }}
				    </div>
			  	</div>
			  	<div class="control-group">
				    <label class="control-label" for="role">{{ Str::title(__('global.password')->get()) }}</label>
				    <div class="controls">
				      {{ Form::span2_text('su_password',null,array('required','class' => 'disabled','readonly')) }}
				    </div>
			  	</div>
	  		</div>
	  	</div>
		{{ Form::close()}}
	  </div>
	  <div class="modal-footer">
  			<button class="btn" data-dismiss="modal" aria-hidden="true">{{ Str::title(__('global.close')->get()) }}</button>
  			<button id="addBtn" class="btn btn-primary">{{ Str::title(__('global.register')->get()) }}</button>
	  </div>
	</div>
	<div id="viewUserModal" class="modal container hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-header">
	    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
	    <h3 id="myModalLabel">{{ Str::title(__('global.userinfo')->get()) }}</h3>
	  </div>
	  <div class="modal-body">

		{{ Form::open('#', 'POST', array('id' => 'editUserForm', 'class' => 'form-horizontal')) }}
	  	<div class="row">
			<div class="span5">
			{{ Form::hidden('su_id')}}
			{{ Form::control_group(Form::label('wp_tarikh_mula', Str::title(__('admin.datestart')->get())),Form::span2_text('wp_tarikh_mula',null,array('required','placeholder'=>Str::title(__('global.type')->get().' '.__('admin.datestart')->get()))),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>'));}}
			{{ Form::control_group(Form::label('wp_tarikh_tamat', Str::title(__('admin.dateend')->get())),Form::span2_text('wp_tarikh_tamat',null,array('placeholder'=>Str::title(__('global.type')->get().' '.__('admin.dateend')->get()))));}}
			{{ Form::control_group(Form::label('sup_id_pengguna', Str::title(__('admin.tagid')->get())),Form::span2_text('sup_id_pengguna',null,array('placeholder'=>Str::title(__('global.type')->get().' '.__('admin.tagid')->get()))),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>'));}}
			{{ Form::control_group(Form::label('sup_nama', Str::title(__('admin.fullname')->get())),Form::span3_text('sup_nama',null,array('placeholder'=>Str::title(__('global.type')->get().' '.__('admin.fullname')->get()))),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>'));}}
			{{ Form::control_group(Form::label('sup_emel', Str::title(__('admin.activeemel')->get())),Form::span3_text('sup_emel',null,array('required','placeholder'=>Str::title(__('global.type')->get().' '.__('admin.activeemel')->get()))),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>'));}}
			{{ Form::control_group(Form::label('sup_tel', Str::title(__('admin.telno')->get())),Form::span2_text('sup_tel',null,array('placeholder'=>Str::title(__('global.type')->get().' '.__('admin.telno')->get()))));}}
			{{ Form::control_group(Form::label('sup_no_ic', Str::title(__('admin.idno')->get())),Form::span2_text('sup_no_ic',null,array('required','placeholder'=>Str::title(__('global.type')->get().' '.__('admin.idno')->get()))),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>'));}}
			{{ Form::control_group(Form::label('sup_no_pekerja', Str::title(__('admin.staffno')->get())),Form::span2_text('sup_no_pekerja',null,array('placeholder'=>Str::title(__('global.type')->get().' '.__('admin.staffno')->get()))));}}
			</div>
			<div class="span5">
			{{ Form::control_group(Form::label('wjkp_no_auto', Str::title(__('admin.staffgroup')->get())),Form::span3_select('wjkp_no_auto', $allgroup));}}
			{{ Form::control_group(Form::label('wpj_no_auto', Str::title(__('admin.department')->get())),Form::span3_select('wpj_no_auto', $alldept));}}
			{{ Form::control_group(Form::label('wpb_no_auto', Str::title(__('admin.division')->get())),Form::span3_select('wpb_no_auto', $division));}}
			{{ Form::control_group(Form::label('wpu_no_auto', Str::title(__('admin.unit')->get())),Form::span3_select('wpu_no_auto', $unit));}}
			{{ Form::control_group(Form::label('sup_penyelia_satu', Str::title(__('admin.supervisor1')->get())),Form::span3_select('sup_penyelia_satu', $supervisorone));}}
			{{ Form::control_group(Form::label('sup_penyelia_dua', Str::title(__('admin.supervisor2')->get())),Form::span3_select('sup_penyelia_dua', $supervisortwo));}}
			{{ Form::control_group(Form::label('sur_id', Str::title(__('admin.navuserrole')->get())),Form::span2_select('sur_id', $allrole),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>'));}}
			{{ Form::control_group(Form::label('gantung', ''),Form::labelled_checkbox('gantung', 'Gantung'));}}
			{{ Form::control_group(Form::label('sup_tidak_kelihatan', ''),Form::labelled_checkbox('sup_tidak_kelihatan', 'Tidak termasuk di dalam sistem'));}}
				<div id="peranan">
				{{ Form::control_group(Form::label('sup_is_supervisor', ''),Form::labelled_checkbox('sup_is_supervisor', 'Adalah Penyelia'));}}
				</div>
			<!-- {{ Form::control_group(Form::label('su_status', Str::title('Status')),Form::xlarge_select('su_status', array('1'=>'Active','2'=>'Deactive')));}} -->
			</div>
	  	</div>
	  	{{ Form::close()}}

	  </div>
	  <div class="modal-footer">
  			<button class="btn" data-dismiss="modal" aria-hidden="true">{{ Str::title(Lang::line('global.close')->get()) }}</button>
  			<button id="editBtn" class="btn btn-primary">{{ Str::title(Lang::line('global.edit')->get()) }}</button>
	  </div>
	</div>
@endsection
@section('scripts')
<script>

var $add = $('#addUserModal');
var $edit = $('#viewUserModal');

	$(function() {
		
		$( '#addUserForm input[name="wp_tarikh_mula"], input[name="wp_tarikh_tamat"]' ).datepicker({
	      	showWeek: true,
	      	firstDay: 1,
        	format:"d-m-yyyy",
      		autoclose: true,
	      	showButtonPanel: true
	    });
		
		$( '#editUserForm input[name="wp_tarikh_mula"], input[name="wp_tarikh_tamat"]' ).datepicker({
	      	showWeek: true,
	      	firstDay: 1,
        	format:"d-m-yyyy",
      		autoclose: true,
	      	showButtonPanel: true
	    });
  	});
	
	$('#addUserForm input[name="sup_no_ic"], input[name="sup_emel"]').keyup(function() {
	  	var emel = $('#addUserForm input[name="sup_emel"]').val();
	  	var icno = '{{ Str::random(16, "alpha") }}';

	  	$('#addUserForm input[name="su_username"]').val(emel);
	  	$('#addUserForm input[name="su_password"]').val(icno);
	})

	$('#addBtn').click(function() {
		$.post('register', $("#addUserForm").serialize(),function(data) {
			      sourcedata = data;
			    }).success(function() { 
			    	sourcedata = jQuery.parseJSON(sourcedata);
			    	if(sourcedata.messages){
			    		validated("addUserForm",sourcedata.messages);
			    	}else{
			    		$( "#userList" ).empty().append( sourcedata );
			    		$("#addUserForm :input").val('');
						$('#addUserModal').modal('hide');
						window.location.replace('{{ url("administrator/user/list")}}');
			    	}
			    }); 
	});

	function viewUser(id, status, peranan){
		$('#viewUserModal').modal('show');

		$.get('userinfo', { id: id, peranan: peranan},function(data,status){
			for (x in data){
				if(x == 'sur_id' || x == 'wpj_no_auto' ){
					$("#editUserForm [name=" + x + "]").val(data[x]);
				}else if( x == 'sup_penyelia_satu'){
					var sup_penyelia_satu = data['sup_penyelia_satu'];
			      	var $elspv1 =  $('#viewUserModal .modal-body #sup_penyelia_satu');
			      	$elspv1.empty();

			  	    $.get("supervisorOne",{ jabatan:data['wpj_no_auto'], bahagian:data['wpb_no_auto'], unit:data['wpu_no_auto']}, function(data,status){
				    	$elspv1.empty();	
				        $.each(data, function(key, value) {
				        	var selected = false;

				          	if(key == sup_penyelia_satu){
				            	selected = true;
				          	}
							$elspv1.append($("<option></option>").attr("value", key).attr("selected", selected).text(value));
				        });
				    },"json");
					
			  	    $("#editUserForm [name=" + x + "]").val(data[x]);						
			  	}else if( x == 'sup_penyelia_dua'){
			  		var sup_penyelia_dua = data['sup_penyelia_dua'];
				    var $elspv2 =  $('#viewUserModal .modal-body #sup_penyelia_dua');
				    $elspv2.empty();

			  	    $.get("supervisorTwo",{ jabatan:data['wpj_no_auto'], bahagian:data['wpb_no_auto'], unit:data['wpu_no_auto'], exclude:data['sup_penyelia_satu']}, function(data,status){
				    	$elspv2.empty();
				        $.each(data, function(key, value) {
				        	var selected = false;
							
							if(key == sup_penyelia_dua){
				            	selected = true;
				          	}
				          	$elspv2.append($("<option></option>").attr("value", key).attr("selected", selected).text(value));
				        });
				    },"json");

				    $("#editUserForm [name=" + x + "]").val(data[x]);
				}else if(x == 'wpb_no_auto'){				        
				    var bahagian = data['wpb_no_auto'];

				    $.get("divlist",{ id: data['wpj_no_auto']}, function(data,status){
				      	var $el = $('#editUserForm [name="wpb_no_auto"]');
				        $el.empty();

				        $.each(data, function(key, value) {
							var selected = false;
							
							if(key == bahagian){
								selected = true;
							}

							$el.append($("<option></option>").attr("value", key).attr("selected", selected).text(value));
				        });
				    },"json");

				    $("#editUserForm [name=" + x + "]").val(data[x]);
				}else if(x == 'wpu_no_auto'){				        
				    var unit = data['wpu_no_auto'];

				    $.get("unitlist",{dept:data['wpj_no_auto'], div:data['wpb_no_auto']}, function(data,status){
				      	var $el = $('#editUserForm [name="wpu_no_auto"]');
				        $el.empty();

				        $.each(data, function(key, value) {
							var selected = false;
							
							if(key == unit){
								selected = true;
							}

							$el.append($("<option></option>").attr("value", key).attr("selected", selected).text(value));
				        });
				    },"json");

				    $("#editUserForm [name=" + x + "]").val(data[x]);
				//}else if(x == 'su_status' && data[x] == 'Aktif'){
					//console.log(data['su_status']);
					//$('#sup_is_supervisor').modal('hide');
					//$('#viewUserModal .modal-body #sup_is_supervisor').modal('hide');							
				}else if(x == 'su_status' && data[x] == 'Gantung'){
					$('#gantung').prop('checked', true);
				}else if(x == 'sup_tidak_kelihatan'){
					if(parseInt(data['sup_tidak_kelihatan']) != 1){
						$('#editUserForm #sup_tidak_kelihatan').prop('checked', false);
					}else{
						$('#editUserForm #sup_tidak_kelihatan').prop('checked', true);
					}					
				}else if(x == 'sup_is_supervisor'){	
					if(parseInt(data['sup_is_supervisor']) != 1){
						$('#editUserForm #sup_is_supervisor').prop('checked', false);
					}else{	
						$('#editUserForm #sup_is_supervisor').prop('checked', true);
					}					
								
				} else{
					$('#editUserForm input[name="'+ x +'"]' ).val(data[x]);
					$('#editUserForm [name="'+ x +'"]' ).val(data[x]);
				}

				if(peranan == 4) {
					$('#peranan').hide();
				}else if(peranan == 1 || peranan == 2 || peranan == 3) {
					$('#peranan').show();
				}

			}
		},"json");
	}

	$('#addUserForm [name="wpj_no_auto"]').change(function() {
		$('body').modalmanager('loading');
		$add.modal('loading');
	  	var jabatan = $('#addUserForm [name="wpj_no_auto"]').val();
      	var $eldept =  $('#addUserForm [name="wpb_no_auto"]');
		var $elunit =  $('#addUserForm [name="wpu_no_auto"]');
        $elunit.empty();
		
		var $elspv2 =  $('#addUserForm [name="sup_penyelia_dua"]');

	    $.get("supervisorTwo",{ jabatan:this.value}, function(data,status){
			$('body').modalmanager('loading');
			//$add.modal('loading');
	    	$elspv2.empty();
			
	        $.each(data, function(key, value) {
				$elspv2.append($("<option></option>").attr("value", key).text(value));
	        });
	    },"json");

	    $.get("divlist",{ id: jabatan}, function(data,status){
			$('body').modalmanager('loading');
	    	$eldept.empty();
			
	        $.each(data, function(key, value) {
				$eldept.append($("<option></option>").attr("value", key).text(value));
	        });
			
	        bahagian = $('#addUserForm [name="wpb_no_auto"] option').size();

		  	if(bahagian == 1){
		      	$.get("unitlist",{ dept:jabatan, div:0}, function(data,status){
					$('body').modalmanager('loading');
					
			        $.each(data, function(key, value) {
						$elunit.append($("<option></option>").attr("value", key).text(value));
			        });
		      	},"json");
		  	}
	    },"json");
		$add.modal('loading');
	});
	
	$('#addUserForm [name="wpb_no_auto"]').change(function() {
		$('body').modalmanager('loading');
		$add.modal('loading');
	  	var jabatan = $('#addUserForm [name="wpj_no_auto"]').val();
		
      	var $elspv1 =  $('#addUserForm [name="sup_penyelia_satu"]');

  	    $.get("supervisorOne",{ jabatan: jabatan, bahagian: this.value}, function(data,status){
			$('body').modalmanager('loading');
			//$add.modal('loading');
	    	$elspv1.empty();
			
	        $.each(data, function(key, value) {
				$elspv1.append($("<option></option>").attr("value", key).text(value));
	        });
	    },"json");

      	$.get("unitlist",{ dept:jabatan, div:this.value}, function(data,status){
			$('body').modalmanager('loading');
	      	var $el = $('#addUserForm [name="wpu_no_auto"]');
	        $el.empty();

	        $.each(data, function(key, value) {
				$el.append($("<option></option>").attr("value", key).text(value));
	        });
      	},"json");
		$add.modal('loading');
	})
	
	$('#addUserForm [name="wpu_no_auto"]').change(function() {
		$('body').modalmanager('loading');
		$add.modal('loading');
		var jabatan = $('#addUserForm [name="wpj_no_auto"]').val();
		var bahagian = $('#addUserForm [name="wpb_no_auto"]').val();

      	var $elspv1 =  $('#addUserForm [name="sup_penyelia_satu"]');

  	    $.get("supervisorOne",{ jabatan:jabatan, bahagian:bahagian, unit:this.value}, function(data,status){
			$('body').modalmanager('loading');
			//$add.modal('loading');
	    	$elspv1.empty();
			
	        $.each(data, function(key, value) {
	          $elspv1.append($("<option></option>").attr("value", key).text(value));
	        });
	    },"json");
		$add.modal('loading');
	})
		
	$('#addUserForm [name="sur_id"]').change(function() {		
		if(this.value == 4) { //pengguna
			$('#pengguna').hide();
			$('#addUserForm input:checkbox[name=sup_is_supervisor]').prop('checked',false);
		} else if(this.value == 3)  { //penyelia
			$('#pengguna').show();
			$('#addUserForm input:checkbox[name=sup_is_supervisor]').prop('checked',true);
		} else {
			$('#pengguna').show();
			$('#addUserForm input:checkbox[name=sup_is_supervisor]').prop('checked',false);
		}
	})
	
	/*$('#addUserForm [name="sup_penyelia_satu"]').change(function() {
      	
		var jabatan = $('#addUserForm [name="wpj_no_auto"]').val();
		var bahagian = $('#addUserForm [name="wpb_no_auto"]').val();
		var unit = $('#addUserForm [name="wpu_no_auto"]').val();

      	var $elspv2 =  $('#addUserForm [name="sup_penyelia_dua"]');

  	    $.get("supervisor",{ jabatan:jabatan, bahagian:bahagian, unit:unit, exclude:this.value}, function(data,status){
	    	console.log(data);
	    	$elspv2.empty();
	        $.each(data, function(key, value) {
	          $elspv2.append($("<option></option>")
	             .attr("value", key).text(value));
	        });

	    },"json");
	})*/
	
	$('#viewUserModal .modal-body #wpj_no_auto').change(function() {
		$('body').modalmanager('loading');
		
	  	var jabatan = $('#viewUserModal .modal-body #wpj_no_auto').val();		
      	var $eldept = $('#viewUserModal .modal-body #wpb_no_auto');
		var $elunit = $('#viewUserModal .modal-body #wpu_no_auto');
        $elunit.empty();

        var bahagian = 0;
		
		var $elspv2 =  $('#viewUserModal .modal-body #sup_penyelia_dua');

	    $.get("supervisorTwo",{ jabatan:this.value}, function(data,status){
			$('body').modalmanager('loading');
			$edit.modal('loading');
	    	$elspv2.empty();
			
	        $.each(data, function(key, value) {
				$elspv2.append($("<option></option>").attr("value", key).text(value));
	        });
	    },"json");

	    $.get("divlist",{ id: jabatan}, function(data,status){
			$('body').modalmanager('loading');
			$eldept.empty();
			
			$.each(data, function(key, value) {
				$eldept.append($("<option></option>").attr("value", key).text(value));
			});

	        bahagian = $('#viewUserModal .modal-body #wpb_no_auto option').size();

		  	if(bahagian == 1){
		      	$.get("unitlist",{ dept:jabatan, div:0}, function(data,status){
					$('body').modalmanager('loading');
			        $.each(data, function(key, value) {
						$elunit.append($("<option></option>").attr("value", key).text(value));
			        });
		      	},"json");
		  	}
	    },"json");
		$edit.modal('loading');
	});

	$('#viewUserModal .modal-body #wpb_no_auto').change(function() {
		$('body').modalmanager('loading');
		
	  	var jabatan = $('#viewUserModal .modal-body #wpj_no_auto').val();
	  	var bahagian = $('#viewUserModal .modal-body #wpb_no_auto').val();

	    $.get("unitlist",{ dept:jabatan, div:bahagian}, function(data,status){
			$('body').modalmanager('loading');
			$edit.modal('loading');
	      	var $el = $('#viewUserModal .modal-body #wpu_no_auto');
	        $el.empty();

	        $.each(data, function(key, value) {
				$el.append($("<option></option>").attr("value", key).text(value));
	        });
	    },"json");

     	var $elspv1 =  $('#viewUserModal .modal-body #sup_penyelia_satu');

  	    $.get("supervisorOne",{ jabatan: jabatan, bahagian: this.value}, function(data,status){
			$('body').modalmanager('loading');			
	    	$elspv1.empty();
			
	        $.each(data, function(key, value) {
				$elspv1.append($("<option></option>").attr("value", key).text(value));
	        });
	    },"json");
		$edit.modal('loading');
	})

	$('#viewUserModal .modal-body #wpu_no_auto').change(function() {
		$('body').modalmanager('loading');
		
	  	var jabatan = $('#viewUserModal .modal-body #wpj_no_auto').val();
	  	var bahagian = $('#viewUserModal .modal-body #wpb_no_auto').val();

      	var $elspv1 =  $('#viewUserModal .modal-body #sup_penyelia_satu');

  	    $.get("supervisorOne",{ jabatan:jabatan, bahagian:bahagian, unit:this.value}, function(data,status){
			$('body').modalmanager('loading');
			$edit.modal('loading');
	    	$elspv1.empty();
			
	        $.each(data, function(key, value) {
				$elspv1.append($("<option></option>").attr("value", key).text(value));
	        });
	    },"json");
		$edit.modal('loading');
	})
	
	$('#viewUserModal .modal-body #sur_id').change(function() {
		if(this.value == 4) { //pengguna
			$('#peranan').hide();
			$('#editUserForm #sup_is_supervisor').prop('checked', false);
		} else if(this.value == 3)  { //penyelia
			$('#peranan').show();
			$('#editUserForm #sup_is_supervisor').prop('checked', true);
		} else {
			$('#peranan').show();
			$('#editUserForm #sup_is_supervisor').prop('checked', false);
		}
	})
	
	/*$('#viewUserModal .modal-body #sup_penyelia_satu').change(function() {
      	
	  	var jabatan = $('#viewUserModal .modal-body #wpj_no_auto').val();
	  	var bahagian = $('#viewUserModal .modal-body #wpb_no_auto').val();
	  	var unit = $('#viewUserModal .modal-body #wpu_no_auto').val();

      	var $elspv2 =  $('#viewUserModal .modal-body #sup_penyelia_dua');

  	    $.get("supervisor",{ jabatan:jabatan, bahagian:bahagian, unit:unit, exclude:this.value}, function(data,status){
	    	console.log(data);
	    	$elspv2.empty();
	        $.each(data, function(key, value) {
	          $elspv2.append($("<option></option>")
	             .attr("value", key).text(value));
	        });

	    },"json");
	})*/
	
	$('#editBtn').click(function() {

		$.post('updateUser', $("#editUserForm").serialize(),function(data) {
			      sourcedata = data;
			    }).success(function() { 
			    	sourcedata = jQuery.parseJSON(sourcedata);
			    	if(sourcedata.messages){
			    		validated("editUserForm",sourcedata.messages);
			    	}else{
			    		$( "#userList" ).empty().append( sourcedata );
			    		$("#editUserForm :input").val('');
						$('#viewUserModal').modal('hide');
						window.location.replace('{{ url("administrator/user/list")}}');
			    	}
			    }); 

	});

	function resetAccount(id,name){
		var r = confirm("Adakah anda pasti untuk reset kata laluan Pengguna " +name.toUpperCase());
		
		if (r==true){
			$.post("resetlogin", "id="+id ,function(data) {
				sourcedata = data;
			}).success(function() {
				$( "#userList" ).empty().append( sourcedata );
			});
		}
	}

	function deleteAccount(id,name){
		//console.log('"'+name.toString()+'"');
		var r = confirm("Adakah anda pasti untuk memadam Pengguna " +name.toUpperCase());

		if (r==true){
		    $.post("deleteAccount", "id="+id ,function(data) {
				sourcedata = data;
			}).success(function() {
				sourcedata = jQuery.parseJSON(sourcedata);

				if(sourcedata.fail){
					alert(sourcedata.fail);
				}else{
					$( "#userList" ).empty().append( sourcedata );
				}
			});
		}
	}

	$('#searchButton').click(function() {
	    $.post("list", $("#searchUserForm").serialize(),function(data) {
			sourcedata = data;
		}).success(function() {
			sourcedata = jQuery.parseJSON(sourcedata);
	        $( "#userList" ).empty().append( sourcedata );
	    });
	});
</script>
@endsection